# Include jni
find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})

# Include headers
include_directories(../../include ../../xcomm/include)
set(SWIG_OUTFILE_DIR ${CMAKE_CURRENT_BINARY_DIR}/_javaswig)

# Add swig module
set_property(SOURCE java.i PROPERTY CPLUSPLUS ON)

swig_add_library(xovip LANGUAGE java SOURCES 
java.i
../../src/comm/axi4_async.cpp
../../src/comm/axi4.cpp
../../src/comm/mem.cpp
)

# set buld output directory
set(OVIP_LIB_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../java/ovip)
set_target_properties(xovip PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${SWIG_OUTFILE_DIR})

add_library(xspcomm_so SHARED IMPORTED)
set_target_properties(xspcomm_so PROPERTIES
    IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/../../xcomm/lib/libxspcomm.so
)
add_dependencies(xovip xspcomm_so)
target_link_libraries(xovip xspcomm_so)

# copy file
add_custom_command(
    OUTPUT ${OVIP_LIB_DIR}/xovip.java
    COMMAND ${CMAKE_COMMAND} -E copy
            ${SWIG_OUTFILE_DIR}/libxovip.so
            ${OVIP_LIB_DIR}/libxovip.so.${PROJECT_VERSION}
    COMMAND ${CMAKE_COMMAND} -E copy
            ${CMAKE_CURRENT_BINARY_DIR}/*.java
            ${OVIP_LIB_DIR}/
    DEPENDS xovip
)

add_custom_command(
    OUTPUT ovip_so_lnk
    COMMAND ${CMAKE_COMMAND} -E create_symlink
            "libxovip.so.${PROJECT_VERSION}"
            "libxovip.so"
    WORKING_DIRECTORY ${OVIP_LIB_DIR}
    DEPENDS ${OVIP_LIB_DIR}/xovip.java
)
add_custom_target(
  _DummyTarget_create_ovip_link ALL
  DEPENDS ovip_so_lnk
)
# Install
install(DIRECTORY ${OVIP_LIB_DIR} DESTINATION share/mcv/java USE_SOURCE_PERMISSIONS)
